package My

import (
	"context"
	"db2s/global"
	"db2s/parDef"
	"db2s/ref"
	"strings"
)

func (cm ColumnMe) Comment(ctx context.Context) (result global.Return, err error) {
	var event = "[Comment]"
	defer func() { err = ref.RecoverPanic(event, recover()) }()
	var _, table = parDef.GetExecFuncParameterValueBase(ctx)
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		result, err = parDef.SqlExec(parDef.GetSqlQueryExecFuncParameterValue(ctx, event, columnSysObjectView, getColumnCommentSql,QueryColValManage))
		return parDef.ResultExec(parDef.NewGetExternalExecFuncParameterValue(ctx, event, result, result.Result, table))
	}
}
func (cm ColumnMe) LobMeta(ctx context.Context) (result global.Return, err error) {
	var event = "[LobMeta]"
	defer func() { err = ref.RecoverPanic(event, recover()) }()
	var _, table = parDef.GetExecFuncParameterValueBase(ctx)
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		result, err = parDef.SqlExec(parDef.GetSqlQueryExecFuncParameterValue(ctx, event, columnSysObjectView, getLobColumnSql,QueryColValManage))
		return parDef.ResultExec(parDef.NewGetExternalExecFuncParameterValue(ctx, event, result, result.Result, table))
	}
}
func (cm ColumnMe) BigVarcharMeta(ctx context.Context) (result global.Return, err error) {
	var event = "[BigVarcharMeta]"
	defer func() { err = ref.RecoverPanic(event, recover()) }()
	var _, table = parDef.GetExecFuncParameterValueBase(ctx)
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		result, err = parDef.SqlExec(parDef.GetSqlQueryExecFuncParameterValue(ctx, event, columnSysObjectView, getBigColumnSql,QueryColValManage))
		return parDef.ResultExec(parDef.NewGetExternalExecFuncParameterValue(ctx, event, result, result.Result, table))
	}
}

func virtualColumnTypeProcessing(s any) {

}
func autoIncrementColumnTypeProcessing(s any) {

}
func interColumnTypeValueConversionProcessing(s map[string]any) (r map[string]any, err error) {
	r = s
	for _, length := range []string{"dataLength", "charLength", "dataPrecision", "dataScale"} {
		if charLength, ok := r[length]; ok {
			var carl int64
			if carl, err = stringAnyToInt64(charLength); err != nil {
				err = ref.ErrAddPrintf("MySQL.Meta", err)
				return
			}
			r[length] = carl
		}
	}
	return
}
func specialColumnDataProcessing(s any) (res []parDef.ColumnMeta, err error) {
	if s == nil {
		return
	}
	for _, v := range s.([]map[string]interface{}) {
		var vv = mapNullActive(v)
		var dd = parDef.ColumnMeta{}
		if vv, err = interColumnTypeValueConversionProcessing(vv); err != nil {
			return
		}
		switch val := vv["autoIncrement"]; val.(type) {
		case bool:
			dd.AutoIncrement = vv["autoIncrement"].(bool)
		case string:
			switch {
			case strings.EqualFold(val.(string), "AUTO_INCREMENT"):
				dd.AutoIncrement = true
			case strings.HasPrefix(strings.TrimSpace(strings.ToUpper(val.(string))), "ON UPDATE"):
				q := strings.Split(strings.TrimSpace(strings.ToUpper(val.(string))), "ON UPDATE")
				dd.OnUpdate.Exist = true
				dd.OnUpdate.Body = strings.TrimSpace(q[1])
			case strings.HasPrefix(strings.TrimSpace(strings.ToUpper(val.(string))), "VIRTUAL GENERATED"):
				dd.Virtual.StorageMethod = "VIRTUAL"
				dd.Virtual.Exist = true
			case strings.HasPrefix(strings.TrimSpace(strings.ToUpper(val.(string))), "STORED GENERATED"):
				dd.Virtual.StorageMethod = "STORED"
				dd.Virtual.Exist = true
			}
		}
		switch val := vv["virtualBody"]; val.(type) {
		case string:
			var l string
			switch {
			case strings.Contains(strings.ToLower(val.(string)), "_utf8mb4"):
				l = strings.ReplaceAll(strings.ToLower(val.(string)), "_utf8mb4", "")
			case strings.Contains(strings.ToLower(val.(string)), "_utf8mb3"):
				l = strings.ReplaceAll(strings.ToLower(val.(string)), "_utf8mb3", "")
			default:
				l = val.(string)
			}
			dd.Virtual.Body = strings.ReplaceAll(l, "\\'", "'")
		}
		if err = ref.MapToStruct(vv, &dd); err != nil {
			err = ref.ErrAddPrintf("MySQL.Meta", err)
		}
		res = append(res, dd)
	}
	return
}
func (cm ColumnMe) Meta(ctx context.Context) (result global.Return, err error) {
	var event = "[Meta]"
	defer func() { err = ref.RecoverPanic(event, recover()) }()
	var _, table = parDef.GetExecFuncParameterValueBase(ctx)
	select {
	case <-ctx.Done():
		err = ctx.Err()
		return
	default:
		result, err = parDef.SqlExec(parDef.GetSqlQueryExecFuncParameterValue(ctx, event, columnSysObjectView, getColumnMetaSql, QueryColValManage))
		result.Result, err = specialColumnDataProcessing(result.Result)
		return parDef.ResultExec(parDef.NewGetExternalExecFuncParameterValue(ctx, event, result, result.Result, table))
	}
}
